//
//  CZGifRefreshView.swift
//  自定义刷新控件
//
//  Created by Summer on 2017/12/15.
//  Copyright © 2017年 Summer. All rights reserved.
//

import UIKit

private let iconValue: CGFloat = 33

/// 动画刷新
class CZGifRefreshView: CZRefreshView {

    @IBOutlet weak var buildingIconView: UIImageView!
    
    @IBOutlet weak var earthIconView: UIImageView!
    
    @IBOutlet weak var kangarooIconView: UIImageView!
    
    override var viewHeight: CGFloat {
        didSet {
            print("高度\(viewHeight)")
            
            if viewHeight < iconValue {
                return
            }
            
            var scale: CGFloat
            
            if viewHeight > 126 {
                scale = 1
            } else {
                scale = 1 - ((126 - viewHeight) / (126 - iconValue))
            }
            kangarooIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 建筑动画
        buildingIconView.image = UIImage.animatedImage(with: [#imageLiteral(resourceName: "icon_building_loading_1"), #imageLiteral(resourceName: "icon_building_loading_2")], duration: 0.8)
        
        // 地球旋转
        let earthAnimation = CABasicAnimation(keyPath: "transform.rotation")
        earthAnimation.toValue = -2 * Double.pi
        earthAnimation.duration = 3
        earthAnimation.repeatCount = MAXFLOAT
        earthAnimation.isRemovedOnCompletion = false
        earthIconView.layer.add(earthAnimation, forKey: nil)
        
        // 袋鼠动画
        kangarooIconView.image = UIImage.animatedImage(with: [#imageLiteral(resourceName: "icon_small_kangaroo_loading_1"), #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")], duration: 0.1)
        // 设置锚点
        kangarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        // 设置位置
        kangarooIconView.center = CGPoint(x: self.bounds.width * 0.5, y: self.bounds.height - iconValue)
        kangarooIconView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
    }
}
